﻿using System;
using Store.WP7.Controls;
using Store.WP7.ViewModels;
using Store.WP7.Framework.Helpers;
using Microsoft.Phone.Shell;
using System.Windows;

namespace Store.WP7.Views
{
    public partial class OrderDetailsPage : StoreApplicationPage
    {
        public OrderDetailsPage()
        {
            InitializeComponent();
            Loaded += OnPageLoaded;
        }

        private void OnPageLoaded(object sender, RoutedEventArgs e)
        {
            string pageUrl = NavigationService.CurrentSource.ToString();
            if (TileHelper.TileExists(pageUrl))
            {
                ((ApplicationBarIconButton)ApplicationBar.Buttons[0]).Text = "supprimer";
                ((ApplicationBarIconButton)ApplicationBar.Buttons[0]).IconUri = new Uri("/icons/appbar.unpin.rest.png", UriKind.Relative);
            }
            else
            {
                ((ApplicationBarIconButton)ApplicationBar.Buttons[0]).Text = "épingler";
                ((ApplicationBarIconButton)ApplicationBar.Buttons[0]).IconUri = new Uri("/icons/appbar.pin.rest.png", UriKind.Relative);
            }
        }

        private void btnPin_Click(object sender, EventArgs e)
        {
            var viewModel = ViewModelBase as OrderDetailsPageViewModel;
            if (viewModel != null)
            {
                if (TileHelper.TileExists(NavigationService.CurrentSource.ToString()))
                {
                    viewModel.Unpin();
                    ((ApplicationBarIconButton)ApplicationBar.Buttons[0]).Text = "épingler";
                    ((ApplicationBarIconButton)ApplicationBar.Buttons[0]).IconUri = new Uri("/icons/appbar.pin.rest.png", UriKind.Relative);
                }
                else
                {
                    viewModel.Pin();
                }
            }
        }

        private void btnRefresh_Click(object sender, EventArgs e)
        {
            var viewModel = ViewModelBase as OrderDetailsPageViewModel;
            if (viewModel != null)
            {
                viewModel.Refresh();
            }
        }
    }
}
